package com.ai.dmc.index.common.exception;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;

import com.ai.dmc.index.common.data.ErrorCode;


/**
 * 异常处理
 * @author sikong.chen
 *
 */
public class ExceptionHandle {
	private static Logger log = LoggerFactory.getLogger(ExceptionHandle.class);
	
	private static Properties props;
	static {
		try{
			Resource resource = new ClassPathResource("error/error.properties");
			props = PropertiesLoaderUtils.loadProperties(resource);
		}catch(Exception e) {
			log.error("加载error.properties异常，异常信息："+e.getMessage());
		}
	}
	
	public static String getExceptionMsg(String exceptionCode){
		return props.getProperty(exceptionCode);
	}
	
	public static String getExceptionMsg(Exception e){
		Map<String,String> exceptinMap = getExceptionMsgMap(e);
		return exceptinMap.get("resultMsg");
	}
	
	public static Map<String,String> getExceptionMsgMap(Exception e){
		String traceMsg = getExceptionTraceMsg(e);
		log.error("------异常信息："+traceMsg);
		Map<String,String> exceptionMap = new HashMap<String,String>();
		if (e instanceof InvocationTargetException) {
			InvocationTargetException ie = (InvocationTargetException)e;
			Throwable te = ie.getTargetException();
			if (te instanceof SQLException || te instanceof BadSqlGrammarException || te instanceof UncategorizedSQLException) {
				exceptionMap.put("resultCode", ErrorCode.SystemError.DB_ERROR);
				exceptionMap.put("resultMsg", ExceptionHandle.getExceptionMsg(ErrorCode.SystemError.DB_ERROR));
				return exceptionMap;
			}
			
			if (te instanceof IOException) {
				exceptionMap.put("resultCode", ErrorCode.SystemError.IO_ERROR);
				exceptionMap.put("resultMsg", ExceptionHandle.getExceptionMsg(ErrorCode.SystemError.IO_ERROR));
				return exceptionMap;
			}
			
			if(te instanceof BusinessException){
				BusinessException businessException = (BusinessException)te;
				exceptionMap.put("resultCode", businessException.getCode());
				exceptionMap.put("resultMsg", ExceptionHandle.getExceptionMsg(businessException.getCode()));
				return exceptionMap;
			}
			
		}
		exceptionMap.put("resultCode",ErrorCode.SystemError.UNKNOW_ERROR);
		exceptionMap.put("resultMsg", ExceptionHandle.getExceptionMsg(ErrorCode.SystemError.UNKNOW_ERROR));
		return exceptionMap;
	}
	
	public static String getExceptionTraceMsg(Exception e){
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		try{
			e.printStackTrace(pw);
		}finally{
			pw.close();
		}
		return sw.toString();
	}
}
